package com.example.oppotest.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.oppotest.entity.youku.AdInfo;
import com.example.oppotest.entity.youku.Effpoint;
import com.example.oppotest.entity.youku.youkuCount;
import com.example.oppotest.mapper.EffpointMapper;
import com.example.oppotest.util.NetUtils;
import com.example.oppotest.util.TimeConversion;
import com.example.oppotest.util.xianyu.ClickpostUtils;
import com.example.oppotest.util.youku.MapStrUtil;
import com.example.oppotest.util.youku.Md5Util;
import com.example.oppotest.util.youku.SignUtil;
import com.example.oppotest.util.youkuUtils;
import org.apache.commons.collections4.map.ListOrderedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.*;


/**
 * @Author wyk
 * @Date: 2025/2/26 22:50
 */

@Service
public class EffpointService {

    @Autowired
    private EffpointMapper effpointMapper;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public List<youkuCount> selectyoukuCountAll() {
        return effpointMapper.selectyoukuCountAll();
    }

    public youkuCount selectyoukuCount(Map<String, String> map) {
        return effpointMapper.selectyoukuCount(map);
    }

    public int insertyoukuCount(youkuCount youkuCount) {
        return effpointMapper.insertyoukuCount(youkuCount);
    }

    public int updateyoukuCount(youkuCount youkuCount) {
        return effpointMapper.updateyoukuCount(youkuCount);
    }

    public List<AdInfo> selectAdInfo() {
        return effpointMapper.selectAdInfo();
    }

    public List<Effpoint> selectEffpoint() {
        return effpointMapper.selectEffpoint();
    }

    public Effpoint selectEffpointLimit(Integer id) {
        return effpointMapper.selectEffpointLimit(id);
    }

    public ModelMap createEffpoint(Effpoint effpoint, HttpServletRequest request) {
        ModelMap map = new ModelMap();
        if (effpointMapper.insertEffpoint(effpoint) > 0) {
            // 获取请求的域名
            String scheme = request.getScheme();
            String serverName = request.getServerName();
            int serverPort = request.getServerPort();

            Integer id = effpointMapper.selectMaxEff();
            Effpoint updateEff = new Effpoint();
            updateEff.setId(id);
            String landingUrl = "", trackUrl = "";
            if (scheme.equals("https") && serverPort == 443) {
                landingUrl = scheme + "://" + serverName + "/page/landing/v2/Android/" + id;
                trackUrl = scheme + "://" + serverName + "/page/track/v2/Android/" + id;
            } else {
                landingUrl = scheme + "://" + serverName + ":" + serverPort + "/page/landing/v2/Android/" + id;
                trackUrl = scheme + "://" + serverName + ":" + serverPort + "/page/track/v2/Android/" + id;
            }
            updateEff.setUrl(landingUrl + "," + trackUrl);
            effpointMapper.updateEffpoint(updateEff); // 更新联调链接
            map.put("success", true);
            map.put("msg", "后台创建链接成功");
        } else {
            map.put("success", false);
            map.put("msg", "后台创建信息失败，请联系管理员");
        }
        return map;
    }

    public ModelMap createEffpointString(AdInfo adInfo, HttpServletRequest request) throws IOException {
        ModelMap map = new ModelMap();
        Effpoint effpoint = selectEffpointLimit(adInfo.getEffpointId());
        if (effpoint == null) {
            map.put("success", false);
            map.put("msg", "查无内部有效监控链接，请联系管理员");
            map.put("result", "查无内部有效监控链接，请联系管理员");
            return map;
        }
        if (effpoint.getLid() != null && effpoint.getXianyuUrl() != null && effpoint.getChoose() != null) {
            // 如果有添加闲鱼参数 则转化因子限制闲鱼（流程：先给闲鱼，闲鱼返回callback，再给优酷）
            // 先给闲鱼联调
            try {
                String adUrl = "";
                if (effpoint.getChoose().equals("0")) {
                    adUrl = ClickpostUtils.showUrl;
                } else {
                    adUrl = ClickpostUtils.clickUrl;
                }
                Map<String, Object> adParams = new ListOrderedMap<>();
                String oa = "";
                if (adInfo.getOs().equals("1") && StringUtils.hasText(adInfo.getOaid())) {
                    oa = "0";
                } else if (adInfo.getOs().equals("0") && StringUtils.hasText(adInfo.getCaid())) {
                    oa = "1";
                }
                adParams.put("lid", effpoint.getLid());
                adParams.put("guid", URLEncoder.encode(adInfo.getTrackId(), "UTF-8")); // trackId
                adParams.put("advertiserId", "aid01");
                adParams.put("planId", "pid01");
                adParams.put("groupId", "uid01");
                adParams.put("creativeId", URLEncoder.encode(adInfo.getCreativeId(), "UTF-8"));
                adParams.put("os", URLEncoder.encode(oa, "UTF-8"));
                adParams.put("ua", URLEncoder.encode(adInfo.getUa(), "UTF-8"));
                adParams.put("ip", URLEncoder.encode(adInfo.getIp(), "UTF-8"));
//                adParams.put("idfaX", StringUtils.hasText(adInfo.getIdfa()) ? URLEncoder.encode(adInfo.getIdfa(), "UTF-8") : "");
//                adParams.put("imeiX", StringUtils.hasText(adInfo.getImei()) ? URLEncoder.encode(adInfo.getImei(), "UTF-8") : "");
                if (StringUtils.hasText(adInfo.getOaid())) {
                    // 使用 SHA-256 哈希算法
                    MessageDigest md = MessageDigest.getInstance("SHA-256");
                    byte[] hashBytes = md.digest(adInfo.getOaid().getBytes());
                    UUID uuid = Md5Util.bytesToUuid(hashBytes);
                    adParams.put("oaid", uuid.toString()); // AndroidQ及更⾼版本的设备号，32位
                    adParams.put("oaidX", Md5Util.MD5(adInfo.getOaid())); // Android的oaid 原文MD5值
                } else {
                    adParams.put("oaid", "");
                    adParams.put("oaidX", "");
                }
                adParams.put("ts", TimeConversion.dateToLong());
                // 获取请求的域名
                String scheme = request.getScheme();
                String serverName = request.getServerName();
                int serverPort = request.getServerPort();
                String callback = "";
                if (scheme.equals("https") && serverPort == 443) {
                    callback = scheme + "://" + serverName + "/clickpost/callback?"
                            + "guid=" + URLEncoder.encode(adInfo.getTrackId(), "UTF-8") + "&"
                            + "cid=" + URLEncoder.encode(adInfo.getClickid(), "UTF-8") + "&"
                            + "CREATIVE01=" + URLEncoder.encode(adInfo.getCreativeId(), "UTF-8") + "&"
                            + "eid=" + URLEncoder.encode(adInfo.getEffpointId().toString(), "UTF-8");
                } else {
                    callback = scheme + "://" + serverName + ":" + serverPort + "/clickpost/callback?"
                            + "guid=" + URLEncoder.encode(adInfo.getTrackId(), "UTF-8") + "&"
                            + "cid=" + URLEncoder.encode(adInfo.getClickid(), "UTF-8") + "&"
                            + "CREATIVE01=" + URLEncoder.encode(adInfo.getCreativeId(), "UTF-8") + "&"
                            + "eid=" + URLEncoder.encode(adInfo.getEffpointId().toString(), "UTF-8");
                }
                adParams.put("callback", URLEncoder.encode(callback, "UTF-8"));
                logger.info("发起闲鱼请求：" + adUrl + "?" + NetUtils.urlencode(adParams));
                String xyResult = NetUtils.requestUrl(adUrl, adParams);
                adInfo.setXyResult(xyResult);
                map.put("success", true);
                map.put("msg", "闲鱼联调成功");
            } catch (Exception e) {
                logger.info("闲鱼联调失败:" + e.getMessage());
                map.put("success", false);
                map.put("msg", "闲鱼联调失败:" + e.getMessage() + ",数据为：" + adInfo);
            }
        } else {
            // 没有其他参数 则转化因子限制优酷
            // 判断转化因子，查看当前时间内优酷记数是否创建，有创建的话数量是否超过转化因子，如果不超过则回转优酷
            Map ykMap = new HashMap();
            ykMap.put("effpointId", effpoint.getId());
            ykMap.put("createTime", TimeConversion.dateToString(new Date()).substring(0, 10)); // 假设2025-03-14
            youkuCount youkuCount = effpointMapper.selectyoukuCount(ykMap);
            if (youkuCount == null) {
                // 先给优酷联调
                Map<String, Object> params = new ListOrderedMap<>();
                params.put("event_type", effpoint.getEvent_type());
                params.put("deep_conv", effpoint.getDeep_conv());
                params.put("event_time", TimeConversion.dateToLong());
                params.put("track_id", adInfo.getTrackId());
                params.put("creative_id", adInfo.getCreativeId());
                String content = MapStrUtil.toStr(params);
                String signature = SignUtil.sign(effpoint.getAppKey(), effpoint.getToken(), content); // 签名

                Map<String, Object> sendParams = new ListOrderedMap<>();
                sendParams.put("appKey", effpoint.getAppKey());
                sendParams.put("signature", signature);
                sendParams.put("content", params);

                // 优酷API联调
                String result = NetUtils.postByRaw(youkuUtils.passbackUrl, JSON.toJSONString(sendParams));
                logger.info(adInfo.getEffpointId() + "、优酷API回传C：" + result);
                JSONObject jsonObject = JSONObject.parseObject(result) ;
                // 对联调成功数据进行处理
                adInfo.setResult(result);
                adInfo.setStatus(jsonObject.getString("success"));
                if (jsonObject.getString("success").equals("true")) {
                    // 创建优酷记数
                    youkuCount newCount = new youkuCount();
                    newCount.setEffpointId(adInfo.getEffpointId());
                    newCount.setCreateTime(TimeConversion.dateToString(new Date()).substring(0, 10));
                    newCount.setAllNum(1);
                    newCount.setFakeNum(1);
                    effpointMapper.insertyoukuCount(newCount);

                    map.put("success", true);
                    map.put("msg", "联调数据获取成功:" + result);
                } else {
                    map.put("success", false);
                    map.put("msg", jsonObject.getString("msg"));
                    map.put("msgCode", jsonObject.getString("msgCode"));
                    map.put("data", jsonObject.getString("data"));
                    map.put("result", result);
                }
            } else {
                // 如果youkuCount存在，则判断当前总数是否超过转化因子 allNum%100 ，也就是余数
                // 换种算法，使用随机数，随机数小与转化因子，就联调
                Random random = new Random();
                if (random.nextInt(100) < effpoint.getTransform()) {
                    // 先给优酷联调
                    Map<String, Object> params = new ListOrderedMap<>();
                    params.put("event_type", effpoint.getEvent_type());
                    params.put("deep_conv", effpoint.getDeep_conv());
                    params.put("event_time", TimeConversion.dateToLong());
                    params.put("track_id", adInfo.getTrackId());
                    params.put("creative_id", adInfo.getCreativeId());
                    String content = MapStrUtil.toStr(params);
                    String signature = SignUtil.sign(effpoint.getAppKey(), effpoint.getToken(), content); // 签名

                    Map<String, Object> sendParams = new ListOrderedMap<>();
                    sendParams.put("appKey", effpoint.getAppKey());
                    sendParams.put("signature", signature);
                    sendParams.put("content", params);

                    // 优酷API联调
                    String result = NetUtils.postByRaw(youkuUtils.passbackUrl, JSON.toJSONString(sendParams));
                    logger.info(adInfo.getEffpointId() + "、优酷API回传：" + result);
                    JSONObject jsonObject = JSONObject.parseObject(result) ;
                    // 对联调成功数据进行处理
                    adInfo.setResult(result);
                    adInfo.setStatus(jsonObject.getString("success"));
                    if (jsonObject.getString("success").equals("true")) {
                        // 更新优酷记数
                        youkuCount updateCount = new youkuCount();
                        updateCount.setEffpointId(youkuCount.getEffpointId());
                        updateCount.setCreateTime(youkuCount.getCreateTime());
                        updateCount.setAllNum(youkuCount.getAllNum() + 1);
                        updateCount.setFakeNum(youkuCount.getFakeNum() + 1);
                        effpointMapper.updateyoukuCount(updateCount);

                        map.put("success", true);
                        map.put("msg", "联调数据获取成功:" + result);
                    } else {
                        map.put("success", false);
                        map.put("msg", jsonObject.getString("msg"));
                        map.put("msgCode", jsonObject.getString("msgCode"));
                        map.put("data", jsonObject.getString("data"));
                        map.put("result", result);
                    }
                } else {
                    youkuCount updateCount = new youkuCount();
                    updateCount.setEffpointId(youkuCount.getEffpointId());
                    updateCount.setCreateTime(youkuCount.getCreateTime());
                    updateCount.setAllNum(youkuCount.getAllNum() + 1);
                    effpointMapper.updateyoukuCount(updateCount);

                    map.put("success", true);
                    map.put("msg", "单独优酷联调数据获取成功,但不回传");
                }
            }
        }
        return map;
    }

    public ModelMap createEffpointString2(AdInfo adInfo, HttpServletRequest request) throws Exception {
        ModelMap map = new ModelMap();
        Effpoint effpoint = selectEffpointLimit(adInfo.getEffpointId());
        if (effpoint == null) {
            map.put("success", false);
            map.put("msg", "查无内部有效监控链接，请联系管理员");
            map.put("result", "空");
            return map;
        }
        if (effpoint.getLid() != null && effpoint.getXianyuUrl() != null && effpoint.getChoose() != null) {
            // 如果有添加闲鱼参数 则转化因子限制闲鱼
            // 判断转化因子，查看当前时间内优酷记数是否创建，有创建的话数量是否超过转化因子，如果不超过则回转优酷
            Map ykMap = new HashMap();
            ykMap.put("effpointId", effpoint.getId());
            ykMap.put("lid", effpoint.getLid());
            ykMap.put("createTime", TimeConversion.dateToString(new Date()).substring(0, 10)); // 假设2025-03-14
            youkuCount youkuCount = effpointMapper.selectyoukuCount(ykMap);
            // 先给优酷联调
            Map<String, Object> params = new ListOrderedMap<>();
            params.put("event_type", effpoint.getEvent_type());
            params.put("deep_conv", effpoint.getDeep_conv());
            params.put("event_time", TimeConversion.dateToLong());
            params.put("track_id", adInfo.getTrackId());
            params.put("creative_id", adInfo.getCreativeId());
            String content = MapStrUtil.toStr(params);
            String signature = SignUtil.sign(effpoint.getAppKey(), effpoint.getToken(), content); // 签名

            Map<String, Object> sendParams = new ListOrderedMap<>();
            sendParams.put("appKey", effpoint.getAppKey());
            sendParams.put("signature", signature);
            sendParams.put("content", params);

            // 优酷API联调
            String result = NetUtils.postByRaw(youkuUtils.passbackUrl, JSON.toJSONString(sendParams));
            logger.info(adInfo.getEffpointId() + "、优酷API回传：" + result);
            JSONObject jsonObject = JSONObject.parseObject(result) ;
            // 对联调成功数据进行处理
            adInfo.setResult(result);
            adInfo.setStatus(jsonObject.getString("success"));
//          effpointMapper.insertAdInfo(adInfo); // 新增联调数据

            if (jsonObject.getString("success").equals("true")) {
                if (youkuCount == null) {
                    // 如果是当天第一个记数(不存在youkuCount) 则回传且新建优酷记数
                    String adUrl = "";
                    if (effpoint.getChoose().equals("0")) {
                        adUrl = ClickpostUtils.showUrl;
                    } else {
                        adUrl = ClickpostUtils.clickUrl;
                    }
                    Map<String, Object> adParams = new ListOrderedMap<>();
                    String oa = "", oaid = "", oaidX = "";
                    if (adInfo.getOs().equals("1") && StringUtils.hasText(adInfo.getOaid())) {
                        oa = "1";
                        oaid = adInfo.getOaid();
                        oaidX = Md5Util.MD5(oaid);
                    } else if (adInfo.getOs().equals("0") && StringUtils.hasText(adInfo.getCaid())) {
                        oa = "0";
                        oaid = adInfo.getCaid();
                        oaidX = Md5Util.MD5(oaid);
                    }
                    adParams.put("lid", effpoint.getLid());
                    adParams.put("guid", effpoint.getId().toString());
                    adParams.put("advertiserId", effpoint.getId().toString());
                    adParams.put("planId", effpoint.getId().toString());
                    adParams.put("groupId", effpoint.getId().toString());
                    adParams.put("creativeId", adInfo.getCreativeId());
                    adParams.put("os", oa);
                    adParams.put("ua", adInfo.getUa());
                    adParams.put("ip", adInfo.getIp());
                    adParams.put("oaid", oaid);
                    adParams.put("oaidX", oaidX);
                    adParams.put("ts", TimeConversion.dateToLong());
                    // 获取请求的域名
                    String scheme = request.getScheme();
                    String serverName = request.getServerName();
                    int serverPort = request.getServerPort();
                    String callback = "";
                    if (scheme.equals("https") && serverPort == 443) {
                        callback = scheme + "://" + serverName + "/clickpost/callback";
                    } else {
                        callback = scheme + "://" + serverName + ":" + serverPort + "/clickpost/callback";
                    }
                    adParams.put("callback", callback);
                    String xyResult = NetUtils.requestUrl(adUrl, adParams);
                    logger.info(adInfo.getEffpointId() + "、" + effpoint.getLid() + "、闲鱼API回传：" + xyResult);
                    adInfo.setXyResult(xyResult);
                    // 创建优酷记数
                    youkuCount newCount = new youkuCount();
                    newCount.setEffpointId(adInfo.getEffpointId());
                    newCount.setLid(effpoint.getLid());
                    newCount.setCreateTime(TimeConversion.dateToString(new Date()).substring(0, 10));
                    newCount.setAllNum(1);
                    newCount.setFakeNum(1);
                    effpointMapper.insertyoukuCount(newCount);
                } else {
                    // 如果youkuCount存在，则判断当前总数是否超过转化因子 allNum%100 ，也就是余数
                    if (youkuCount.getAllNum() % 100 < effpoint.getTransform() && youkuCount.getFakeNum() % 100 < effpoint.getTransform()) {
                        String adUrl = "";
                        if (effpoint.getChoose().equals("0")) {
                            adUrl = ClickpostUtils.showUrl;
                        } else {
                            adUrl = ClickpostUtils.clickUrl;
                        }
                        Map<String, Object> adParams = new ListOrderedMap<>();
                        String oa = "", oaid = "", oaidX = "";
                        if (adInfo.getOs().equals("1") && StringUtils.hasText(adInfo.getOaid())) {
                            // 安卓
                            oa = "0";
                            oaid = adInfo.getOaid();
                            oaidX = Md5Util.MD5(oaid);
                        } else if (adInfo.getOs().equals("0") && StringUtils.hasText(adInfo.getCaid())) {
                            // 苹果
                            oa = "1";
                            oaid = adInfo.getCaid();
                            oaidX = Md5Util.MD5(oaid);
                        }
                        adParams.put("lid", effpoint.getLid());
                        adParams.put("guid", effpoint.getId().toString());
                        adParams.put("advertiserId", effpoint.getId().toString());
                        adParams.put("planId", effpoint.getId().toString());
                        adParams.put("groupId", effpoint.getId().toString());
                        adParams.put("creativeId", adInfo.getCreativeId());
                        adParams.put("os", oa);
                        adParams.put("ua", adInfo.getUa());
                        adParams.put("ip", adInfo.getIp());
                        adParams.put("oaid", oaid);
                        adParams.put("oaidX", oaidX);
                        adParams.put("ts", TimeConversion.dateToLong());
                        // 获取请求的域名
                        String scheme = request.getScheme();
                        String serverName = request.getServerName();
                        int serverPort = request.getServerPort();
                        String callback = "";
                        if (scheme.equals("https") && serverPort == 443) {
                            callback = scheme + "://" + serverName + "/clickpost/callback";
                        } else {
                            callback = scheme + "://" + serverName + ":" + serverPort + "/clickpost/callback";
                        }
                        adParams.put("callback", callback);
                        String xyResult = NetUtils.requestUrl(adUrl, adParams);
                        logger.info(adInfo.getEffpointId() + "、" + effpoint.getLid() + "、闲鱼API回传：" + xyResult);
                        adInfo.setXyResult(xyResult);
                        // 更新优酷记数
                        youkuCount updateCount = new youkuCount();
                        updateCount.setEffpointId(youkuCount.getEffpointId());
                        updateCount.setLid(youkuCount.getLid());
                        updateCount.setCreateTime(youkuCount.getCreateTime());
                        updateCount.setAllNum(youkuCount.getAllNum() + 1);
                        updateCount.setFakeNum(youkuCount.getFakeNum() + 1);
                        effpointMapper.updateyoukuCount(updateCount);
                    } else {
                        // 如果余数大于转化因子则更新总数即可，不用回传
                        youkuCount updateCount = new youkuCount();
                        updateCount.setEffpointId(youkuCount.getEffpointId());
                        updateCount.setLid(youkuCount.getLid());
                        updateCount.setCreateTime(youkuCount.getCreateTime());
                        updateCount.setAllNum(youkuCount.getAllNum() + 1);
                        effpointMapper.updateyoukuCount(updateCount);
                    }
                }
                map.put("success", true);
                map.put("msg", "联调数据获取成功");
                map.put("result", result);
            } else {
                map.put("success", false);
                map.put("msg", jsonObject.getString("msg"));
                map.put("msgCode", jsonObject.getString("msgCode"));
                map.put("data", jsonObject.getString("data"));
                map.put("result", result);
            }
        } else {
            // 没有其他参数 则转化因子限制优酷
            // 判断转化因子，查看当前时间内优酷记数是否创建，有创建的话数量是否超过转化因子，如果不超过则回转优酷
            Map ykMap = new HashMap();
            ykMap.put("effpointId", effpoint.getId());
            ykMap.put("createTime", TimeConversion.dateToString(new Date()).substring(0, 10)); // 假设2025-03-14
            youkuCount youkuCount = effpointMapper.selectyoukuCount(ykMap);
            if (youkuCount == null) {
                // 先给优酷联调
                Map<String, Object> params = new ListOrderedMap<>();
                params.put("event_type", effpoint.getEvent_type());
                params.put("deep_conv", effpoint.getDeep_conv());
                params.put("event_time", TimeConversion.dateToLong());
                params.put("track_id", adInfo.getTrackId());
                params.put("creative_id", adInfo.getCreativeId());
                String content = MapStrUtil.toStr(params);
                String signature = SignUtil.sign(effpoint.getAppKey(), effpoint.getToken(), content); // 签名

                Map<String, Object> sendParams = new ListOrderedMap<>();
                sendParams.put("appKey", effpoint.getAppKey());
                sendParams.put("signature", signature);
                sendParams.put("content", params);

                // 优酷API联调
                String result = NetUtils.postByRaw(youkuUtils.passbackUrl, JSON.toJSONString(sendParams));
                logger.info(adInfo.getEffpointId() + "、优酷API回传：" + result);
                JSONObject jsonObject = JSONObject.parseObject(result) ;
                // 对联调成功数据进行处理
                adInfo.setResult(result);
                adInfo.setStatus(jsonObject.getString("success"));
                if (jsonObject.getString("success").equals("true")) {
                    // 创建优酷记数
                    youkuCount newCount = new youkuCount();
                    newCount.setEffpointId(adInfo.getEffpointId());
                    newCount.setCreateTime(TimeConversion.dateToString(new Date()).substring(0, 10));
                    newCount.setAllNum(1);
                    newCount.setFakeNum(1);
                    effpointMapper.insertyoukuCount(newCount);

                    map.put("success", true);
                    map.put("msg", "联调数据获取成功");
                    map.put("result", result);
                } else {
                    map.put("success", false);
                    map.put("msg", jsonObject.getString("msg"));
                    map.put("msgCode", jsonObject.getString("msgCode"));
                    map.put("data", jsonObject.getString("data"));
                    map.put("result", result);
                }
            } else {
                // 如果youkuCount存在，则判断当前总数是否超过转化因子 allNum%100 ，也就是余数
                if (youkuCount.getAllNum() % 100 < effpoint.getTransform() && youkuCount.getFakeNum() % 100 < effpoint.getTransform()) {
                    // 先给优酷联调
                    Map<String, Object> params = new ListOrderedMap<>();
                    params.put("event_type", effpoint.getEvent_type());
                    params.put("deep_conv", effpoint.getDeep_conv());
                    params.put("event_time", TimeConversion.dateToLong());
                    params.put("track_id", adInfo.getTrackId());
                    params.put("creative_id", adInfo.getCreativeId());
                    String content = MapStrUtil.toStr(params);
                    String signature = SignUtil.sign(effpoint.getAppKey(), effpoint.getToken(), content); // 签名

                    Map<String, Object> sendParams = new ListOrderedMap<>();
                    sendParams.put("appKey", effpoint.getAppKey());
                    sendParams.put("signature", signature);
                    sendParams.put("content", params);

                    // 优酷API联调
                    String result = NetUtils.postByRaw(youkuUtils.passbackUrl, JSON.toJSONString(sendParams));
                    logger.info(adInfo.getEffpointId() + "、优酷API回传：" + result);
                    JSONObject jsonObject = JSONObject.parseObject(result) ;
                    // 对联调成功数据进行处理
                    adInfo.setResult(result);
                    adInfo.setStatus(jsonObject.getString("success"));
                    if (jsonObject.getString("success").equals("true")) {
                        // 更新优酷记数
                        youkuCount updateCount = new youkuCount();
                        updateCount.setEffpointId(youkuCount.getEffpointId());
                        updateCount.setCreateTime(youkuCount.getCreateTime());
                        updateCount.setAllNum(youkuCount.getAllNum() + 1);
                        updateCount.setFakeNum(youkuCount.getFakeNum() + 1);
                        effpointMapper.updateyoukuCount(updateCount);

                        map.put("success", true);
                        map.put("msg", "联调数据获取成功");
                        map.put("result", result);
                    } else {
                        map.put("success", false);
                        map.put("msg", jsonObject.getString("msg"));
                        map.put("msgCode", jsonObject.getString("msgCode"));
                        map.put("data", jsonObject.getString("data"));
                        map.put("result", result);
                    }
                } else {
                    // 如果余数大于转化因子则更新总数即可，不用回传
                    youkuCount updateCount = new youkuCount();
                    updateCount.setEffpointId(youkuCount.getEffpointId());
                    updateCount.setCreateTime(youkuCount.getCreateTime());
                    updateCount.setAllNum(youkuCount.getAllNum() + 1);
                    effpointMapper.updateyoukuCount(updateCount);

                    map.put("success", true);
                    map.put("msg", "联调数据获取成功,但不回传");
                }
            }
        }
        return map;
    }

}
